package obj.day5.exam;

import java.util.Objects;

/**
 * @author げっしょく
 * @date 2021/7/21
 */
public class Test3 {
    // 定义Person类，有name和age两个属性。
    //	创建该类的两个对象，并输出对象
    //	使用equals比较两个对象，输出比较结果
    //	重写Object的toString和equals方法后再次执行上述两个结果。
    public static void main(String[] args) {
        Person stu1 = new Person("zhangsan", 20);
        Person stu2 = new Person("zhangsan", 20);
        System.out.println(stu1.equals(stu2));
        System.out.println(stu1);
        System.out.println(stu2);
    }

}

class Person{
    private String name ;
    private int age;

    public Person() {
    }

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Person person = (Person) o;
        return age == person.age &&
                Objects.equals(name, person.name);
    }

}